﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DAL;
using CommontLibrary;
public partial class admin_Default : System.Web.UI.Page
{
#region "Member variables"
    DANguoidung objNguoidung;
    DAHocVien objHocVien;
    DAChucNang objChucNang;
    DALevel objLevel;
    int intUserID;
    int intLevelID = 0;
#endregion
#region "Parametter variables"
    // Ham lay Bind du lieu
    public SqlConnection ConnectionView
    {
        get
        {
            object obj2 = this.ViewState["ConnectionView"];
            if (obj2 != null)
            {
                return (SqlConnection)obj2;
            }
            return null;
        }
        set
        {
            this.ViewState["ConnectionView"] = value;
        }
    }
    #endregion
#region "Private methods"
   
#endregion
#region "Event methods"
    private void loadDataAccess()
    {
        SqlConnection sqlConnec = ConnectData.getConnection();
        objNguoidung = new DANguoidung(sqlConnec);
        objChucNang = new DAChucNang(sqlConnec);
        objHocVien = new DAHocVien(sqlConnec);
    }
    protected void Page_Load(object sender, EventArgs e)
    {

            loadDataAccess();
            if (Session["AccountAdmin"] != null)
            {
                if (Session["AccountAdmin"].ToString().Equals("True"))
                {
                    intUserID = Convert.ToInt32(Session["UserID"].ToString());
                    if (intUserID == 0)
                    {
                        string v_str_user = Session["UserName"].ToString();
                        if (v_str_user.Equals(""))
                            Response.Redirect("~\\DangNhap.aspx");
                        else
                        {
                            intUserID = objNguoidung.loadIdNguoidungByUserName(v_str_user);
                            Session["UserID"] = intUserID;
                            if (intUserID == 0) Response.Redirect("~\\DangNhap.aspx");
                        }
                    }
                    if (Request.QueryString["ChucNang"] == null)
                    {

                        DataSet dsNguoiDung = objNguoidung.loadNguoiDungChucNang(intUserID);
                        foreach (DataRow childRow in dsNguoiDung.Tables[0].Rows)
                        {
                            Response.Redirect(StringUntil.getURLNow(Request.RawUrl.Trim()) + "?ChucNang=" + childRow["ID"].ToString() + "&UserList=" + intUserID.ToString());
                        }
                    }
                    else
                    {
                        if (Request.QueryString["Logout"] != null)
                        {
                            FormsAuthentication.SignOut();
                            Session["AccountAdmin"] = "";
                            Session["Username"] = "";
                            Session.Abandon();
                            Response.Cookies.Remove("UserName");
                            Response.Cookies.Remove("PassWord");
                            Response.Cookies["UserName"].Expires = DateTime.Now.AddMonths(-1);
                            Response.Cookies["PassWord"].Expires = DateTime.Now.AddMonths(-1);
                            Session["AccountAdmin"] = "False";
                            Session["Username"] = "";
                            Response.Redirect("Dangnhap.aspx");
                        }
                       // if (!Utilities.CheckQuyenTruyCap(Session["UserID"].ToString(), Request.QueryString["ChucNang"])) Response.Redirect("~\\default.aspx");
                    
                    
                    }
                    if (Request.QueryString["LevelView"] != null)
                    {
                        bool boolCheck = StringUntil.checkNumber(Request.QueryString["LevelView"].Trim());
                        if (boolCheck)
                        {
                            intLevelID = Convert.ToInt32(Request.QueryString["LevelView"].Trim());
                            Session["LevelID"] = intLevelID;
                        }
                    }

                }
                else
                {
                    Response.Redirect("~\\DangNhap.aspx");
                }
            }
            else
            {
                Response.Redirect("~\\DangNhap.aspx");
            }
       
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
    }  
#endregion
}
